package controllers

import (
	"../models"
	"fmt"
	"github.com/astaxie/beego"
	"strconv"
)

type TestController struct {
	beego.Controller
	controllerName string
	actionName     string
	id             int
	user           *models.User
}

func (this *TestController) Get() {
	this.Ctx.WriteString("这是自动匹配路由")
}

func (this *TestController) Index() {
	values := this.Ctx.Input.Params()
	str := fmt.Sprintln(values)

	this.Ctx.WriteString("这是自动匹配路由 /index , values is " + str)
}

func (this *TestController) Say() {
	values := this.Ctx.Input.Params()
	str := fmt.Sprintln(values)

	this.Ctx.WriteString("/test/say , values is " + str)
}

func (this *TestController) Info() {
	//  http://127.0.0.1:8080/test/info/1
	id, err := strconv.Atoi(this.Ctx.Input.Param(":id"))
	user, err := models.GetById(id)

	if err != nil {
		this.ajaxMsg(err, MSG_ERR)
	}
	this.ajaxMsg(user, MSG_OK)

}

//ajax返回
func (self *TestController) ajaxMsg(msg interface{}, msgno int) {
	out := make(map[string]interface{})
	out["status"] = msgno
	out["message"] = msg
	self.Data["json"] = out
	self.ServeJSON()
	self.StopRun()
}

const (
	MSG_OK  = 0
	MSG_ERR = -1
)
